package com.aiwei.tdjk.service.impl;

import com.aiwei.tdjk.bean.Pager;
import com.aiwei.tdjk.dao.extend.CustomsHandbookUsedDetailExtendMapper;
import com.aiwei.tdjk.dto.request.CustomsHandbookUsedDetailPagerRequest;
import com.aiwei.tdjk.dto.response.CustomsHandbookUsedDetailPagerResponse;
import com.aiwei.tdjk.entity.CustomsHandbookProductdetail;
import com.aiwei.tdjk.entity.CustomsHandbookUsedDetail;
import com.aiwei.tdjk.entity.CustomsHandbookUsedDetailExample;
import com.aiwei.tdjk.enums.CustomsHandbookUsedDetailStatusEnum;
import com.aiwei.tdjk.service.CustomsHandbookProductdetailService;
import com.aiwei.tdjk.service.CustomsHandbookUsedDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomsHandbookUsedDetailServiceImpl implements CustomsHandbookUsedDetailService{

    @Autowired
    private CustomsHandbookUsedDetailExtendMapper customsHandbookUsedDetailExtendMapper;

    @Autowired
    private CustomsHandbookProductdetailService customsHandbookProductdetailService;


    /**
     * 分页查询
     * @param request
     * @return
     */
    @Override
    public Pager<CustomsHandbookUsedDetailPagerResponse> find(CustomsHandbookUsedDetailPagerRequest request) {
        request.webParamConvert();
        List<CustomsHandbookUsedDetailPagerResponse> list = customsHandbookUsedDetailExtendMapper.find(request);
        int totalCount = customsHandbookUsedDetailExtendMapper.getTotalCount(request);
        Pager<CustomsHandbookUsedDetailPagerResponse> pager = new Pager<>();
        pager.setPageNumber(request.getPageNumber());
        pager.setPageSize(request.getPageSize());
        pager.setList(list);
        pager.setTotalCount(totalCount);
        return pager;
    }


    /**
     * 获取
     * @param id
     * @return
     */
    @Override
    public CustomsHandbookUsedDetail get(Long id){
        return customsHandbookUsedDetailExtendMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomsHandbookUsedDetail> getListByDeliveryId(Long deliveryId) {
        CustomsHandbookUsedDetailExample example = new CustomsHandbookUsedDetailExample();
        example.or().andDeliveryIdEqualTo(deliveryId);
        List<CustomsHandbookUsedDetail> customsHandbookUsedDetails = customsHandbookUsedDetailExtendMapper.selectByExample(example);
        return customsHandbookUsedDetails;
    }

    @Override
    public List<CustomsHandbookUsedDetail> getListBySaleOrderHeaderId(Long saleOrderHeaderId) {
        CustomsHandbookUsedDetailExample example = new CustomsHandbookUsedDetailExample();
        example.or().andSaleOrderHeaderIdEqualTo(saleOrderHeaderId);
        List<CustomsHandbookUsedDetail> customsHandbookUsedDetails = customsHandbookUsedDetailExtendMapper.selectByExample(example);
        return customsHandbookUsedDetails;
    }

    @Override
    public List<CustomsHandbookUsedDetail> getListByCustomerHandBookId(Long handBookId) {

        List<Long> ids = getHandbookProductdetailsIDs(handBookId);
        CustomsHandbookUsedDetailExample example = new CustomsHandbookUsedDetailExample();
        if (ids!=null&&ids.size()>0){
            example.or().andCustomsHandbookProductdetailIdIn(ids);
            return customsHandbookUsedDetailExtendMapper.selectByExample(example);
        }
        return null;

    }

    public List<Long> getHandbookProductdetailsIDs(Long handBookId) {
        List<CustomsHandbookProductdetail> handbookProductdetails = customsHandbookProductdetailService.getByCustomsHandBook(handBookId);
        List<Long> ids = new ArrayList<>();
        if (handbookProductdetails!=null&&handbookProductdetails.size()>0){
            for (CustomsHandbookProductdetail customsHandbookProductdetail:handbookProductdetails){
                ids.add(customsHandbookProductdetail.getId());
            }
        }
        return ids;
    }


    /**
     * 新增
     *
     * @param customsHandbookUsedDetail
     * @return
     */
    @Override
    public int add(CustomsHandbookUsedDetail customsHandbookUsedDetail) {
        return customsHandbookUsedDetailExtendMapper.insertSelective(customsHandbookUsedDetail);
    }

    /**
     * 更新
     *
     * @param customsHandbookUsedDetail
     */
    @Override
    public void update(CustomsHandbookUsedDetail customsHandbookUsedDetail) {
        customsHandbookUsedDetailExtendMapper.updateByPrimaryKeySelective(customsHandbookUsedDetail);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        customsHandbookUsedDetailExtendMapper.deleteByPrimaryKey(id);
    }

    /**
     * 获取已经报关的数量
     *
     * @param handBookProductDetailId
     * @
     */
    public int getHasDeclareCountByDetailId(Long handBookProductDetailId) {

        CustomsHandbookUsedDetailExample example = new CustomsHandbookUsedDetailExample();
        example.or().andCustomsHandbookProductdetailIdEqualTo(handBookProductDetailId).andStatusEqualTo(CustomsHandbookUsedDetailStatusEnum.declare.getCode());
        List<CustomsHandbookUsedDetail> customsHandbookUsedDetails = customsHandbookUsedDetailExtendMapper.selectByExample(example);

        if (customsHandbookUsedDetails!=null&customsHandbookUsedDetails.size()>0){
            return customsHandbookUsedDetails.get(0).getUsedQty();
        }
        return 0;
    }

    /**
     * 获取已经商检未报关的数量
     *
     * @param handBookProductDetailId
     */
    public int getHasShangJianCountByDetailId(Long handBookProductDetailId) {
        CustomsHandbookUsedDetailExample example = new CustomsHandbookUsedDetailExample();
        example.or().andCustomsHandbookProductdetailIdEqualTo(handBookProductDetailId).andStatusEqualTo(CustomsHandbookUsedDetailStatusEnum.unDeclare.getCode());
        List<CustomsHandbookUsedDetail> customsHandbookUsedDetails = customsHandbookUsedDetailExtendMapper.selectByExample(example);
        if (customsHandbookUsedDetails!=null&customsHandbookUsedDetails.size()>0){
            return customsHandbookUsedDetails.get(0).getUsedQty();
        }
        return 0;
    }

    @Override
    public int getNotFrozenCountByDetailId(Long handBookProductDetailId) {
        CustomsHandbookProductdetail customsHandbookProductdetail = customsHandbookProductdetailService.get(handBookProductDetailId);
        int declareCountByDetailId = getHasDeclareCountByDetailId(handBookProductDetailId);
        int hasShangJianCountByDetailId = getHasShangJianCountByDetailId(handBookProductDetailId);
        return customsHandbookProductdetail.getRecordQty()-declareCountByDetailId-hasShangJianCountByDetailId;
    }

}
